/**
 *  gulp init
 *
 */
var gulp = require ('gulp')
var webserver = require('gulp-webserver')
var webpack = require('gulp-webpack')
var named = require('vinyl-named')
var sass = require('gulp-sass')
var proxy = require('http-proxy-middleware')

var rev = require('gulp-rev')
var revCollector = require('gulp-rev-collector')
var del = require('del')
var gulpsequence = require('gulp-sequence')
var $ = require('gulp-load-plugins')()

//env swap
var minimist = require('minimist')
var gulpif = require('gulp-if')
var knownOptions = {
  string: 'env',
  default: {env:process.env.NODE_ENV || 'prodcution'}
}
var options = minimist(process.argv.slice(2), knownOptions)

var dev = "development"
var prod = "production"


//启动一个webserver 服务
gulp.task('webserver', function(){
  gulp.src('./dev/')
    .pipe(
      webserver({
        host:'localhost',
        port:8000,
        directoryListing: {
          enable: true,
          path: './dev'
        },
        livereload: true,

        middleware: [
          proxy('/api', {
            target: 'https://m.jiuxian.com/',
            changeOrigin: true,
            pathRewrite: {
              '^/api/ststic': '/m_v1/statics/getzx.htm',
              '^/api/m_v1/dynamic':'/m_v1/dynamic/mob01ajax/151461'
            }
          })
        ]
      })
    )
})

//打包js
gulp.task('packjs', function(){
  gulp.src('./src/script/views/*.js')
    .pipe(named())
    .pipe(webpack({
      output: {
        filename: '[name].js'
      },
      module: {
        loaders:[
          {
            test: /\.js$/,
            loader: 'imports-loader',
            exclude: './node_modules'
          },
          {
            test:/\.string$/,
            loader:'string-loader'
          }
        ]
      }
    }))
    .pipe(gulpif(options.env === prod, $.uglify()))
    .pipe(gulpif(options.env === prod, rev()))
    .pipe(gulpif(options.env === prod, gulp.dest('./build/script')))
    .pipe(gulpif(options.env === dev, gulp.dest('./dev/script')))
    .pipe(gulpif(options.env === prod, rev.manifest()))
    .pipe(gulpif(options.env === prod, gulp.dest('./build/rev/script')))

})

//打包scss
gulp.task('packcss', function(){
  gulp.src('./src/style/usage/module/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulpif(options.env === prod,$.minifyCss()))
    .pipe(gulpif(options.env === prod, rev()))
    .pipe(gulpif(options.env === prod, gulp.dest('./build/style')))
    .pipe(gulpif(options.env === dev, gulp.dest('./dev/style')))
    .pipe(gulpif(options.env === prod, rev.manifest()))
    .pipe(gulpif(options.env === prod, gulp.dest('./build/rev/style')))
})

//pack HTML
gulp.task('packHtml',function() {
  gulp.src(['./build/rev/**/*.json', './build/html/*.html'])
    .pipe(revCollector())
    .pipe($.minifyHtml())
    .pipe(gulp.dest('./build/html'))
})

//拷贝src下的html
gulp.task('copyHtml', function(){
  gulp.src('./src/html/*.html')
    .pipe(gulpif(options.env === dev, gulp.dest('./dev/html')))
    .pipe(gulpif(options.env === prod, gulp.dest('./build/html')))
})

//copy images
gulp.task('copyImage',function(){
  gulp.src('./src/images/*')
  .pipe(gulpif(options.env === dev, gulp.dest('./dev/images')))
  .pipe(gulpif(options.env === prod, gulp.dest('./build/images')))
})

//copy libs
gulp.task('copyLibs', function(){
  gulp.src('./src/script/libs/*')
    .pipe(gulpif(options.env === dev, gulp.dest('./dev/script/libs')))
    .pipe(gulpif(options.env === prod, gulp.dest('./build/script/libs')))
})

//delete build files
gulp.task('clean', del.bind(null,['./build/**/*'], {
  force: true
}))

//pack
gulp.task('pack', function(callback) {
  if (options.env === prod) {
    gulpsequence('clean',['packjs','packcss','copyHtml', 'copyLibs', 'copyImage'], 'packHtml')(callback)
  } else {
    gulpsequence(['packjs','packcss','copyHtml', 'copyLibs', 'copyImage'], 'packHtml')(callback)
  }
})


gulp.task('watch', function(){
  gulp.watch('./src/html/*.html', ['copyHtml'])
  gulp.watch('./src/script/**/*.js', ['packjs'])
  gulp.watch('./src/style/usage/**/*.scss', ['packcss'])
  gulp.watch('./src/images/*', ['copyImage'])
  gulp.watch('./src/script/libs/*', ['copyLibs'])

})


gulp.task('default', ['pack', 'watch', 'webserver'], function () {

})
